//
//  SNBaseInfoManager.m
//  app
//
//  Created by 王少宁 on 15/7/13.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNBaseInfoManager.h"

@interface SNBaseInfoManager ()
@property (nonatomic, copy) NSString * filePath;
@property (nonatomic, retain) NSMutableDictionary * baseInfoDic;
@end

@implementation SNBaseInfoManager

+ (SNBaseInfoManager *)shareManager
{
    AS_SINGLETON(SNBaseInfoManager, manager, {
        manager = [[SNBaseInfoManager alloc] init];
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString * path = [paths objectAtIndex:0];
        manager.filePath = [path stringByAppendingPathComponent:@"baseInfo.plist"];
        NSFileManager* fm = [NSFileManager defaultManager];
        if (![fm fileExistsAtPath:manager.filePath]) {
            [fm createFileAtPath:manager.filePath contents:nil attributes:nil];
        }
        
        manager.baseInfoDic = [NSMutableDictionary dictionaryWithContentsOfFile:manager.filePath];
        if (manager.baseInfoDic == nil) {
            manager.baseInfoDic = [NSMutableDictionary dictionary];
            [manager.baseInfoDic writeToFile:manager.filePath atomically:YES];
        }
    })
    return manager;
}

- (void)setBaseInfoObject:(id)object forKey:(id)key
{
    if (!object) {
        DLog(@" 您需要保存的 object %@ 为 nil",object);
        return;
    }
    
    [self.baseInfoDic setObject:object forKey:key];
    [self.baseInfoDic writeToFile:self.filePath atomically:YES];
}

- (id)baseInfoObjectForKey:(id)key
{
    id object = [self.baseInfoDic objectForKey:key];
    return object;
}

@end
